package cn.edu.sdjzu.cs.dingcan.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import cn.edu.sdjzu.cs.dingcan.dao.OrderDao;
import cn.edu.sdjzu.cs.dingcan.dao.OrderItemDao;
import cn.edu.sdjzu.cs.dingcan.entity.*;
import cn.edu.sdjzu.cs.dingcan.utils.JdbcUtils;
import cn.edu.sdjzu.cs.dingcan.utils.PageBean;

public class OrderService {

	private final OrderDao orderDao;
	private final OrderItemDao orderItemDao;
//	private final DinnerTableService dinnerTableService;

	public OrderService() {
		this.orderDao = new OrderDao();
		this.orderItemDao = new OrderItemDao();
//		this.dinnerTableService = new DinnerTableService();
	}

	/**
	 * 下订单
	 * 
	 * @param customer 	下单顾客
	 * @param cart    	购物车
	 * @throws SQLException
	 */
	public void takeOrder(Customer customer, ShoppingCart cart) throws SQLException {
		// 新建订单对象
		Order order = new Order();

		order.setCustomer(customer);
		order.setItems(cart.convertToOrderItems());
		order.setOrderDate(new Date());
		order.setTotalPrice(cart.calculateTotalPrice());
		order.setStatus(OrderStatus.ORDERED);

		try (Connection conn = JdbcUtils.getDataSource().getConnection();) {
			conn.setAutoCommit(false);	// 开启事务
			Long orderId = this.orderDao.add(order);
			this.orderItemDao.add(conn, orderId, order.getItems());
			conn.commit();				// 提交事务
		} catch(SQLException sqlex) {
			throw new RuntimeException(sqlex);
		}
	}

	/**
	 * 取消订单，暂时只更新状态
	 * 
	 * @param orderId
	 * @throws SQLException
	 */
	public void cancelOrder(Long orderId) throws SQLException {
		Order order = this.orderDao.getOrderById(orderId);
		if(Boolean.TRUE.equals(order.cancelable())) {
			this.orderDao.updateOrderStatus(orderId, OrderStatus.CANCELED);
		}
	}

	/**
	 * 根据订单Id获取订单
	 * @param orderId
	 * @return
	 * @throws SQLException
	 */
	public Order getOrderById(Long orderId) throws SQLException {
		Order order = this.orderDao.getOrderById(orderId);
		List<OrderItem> orderItems = this.orderItemDao.findByOrderId(orderId);
		order.setItems(orderItems);
		return order;
	}

	/**
	 * 获取全部订单
	 * @return 全部订单列表
	 * @throws SQLException
	 */
	public void getAllOrders(PageBean<Order> pageBean) throws SQLException {
		pageBean.setPageData(this.orderDao.findAll());
		//总记录数到pb中
		int totalCount = this.orderDao.getCount();
		pageBean.setTotalCount(totalCount);

		/**
		 * 如果当前页 <= 0;       当前页设置当前页为1;
		 * 如果当前页 > 最大页数；  当前页设置为最大页数;
		 */

		if (pageBean.getCurrentPage() <= 0) {
			pageBean.setCurrentPage(1);
		}
		if (pageBean.getCurrentPage() > pageBean.getTotalPage()) {
			pageBean.setCurrentPage(pageBean.getTotalPage());
		}
	}

	/**
	 * 结账
	 * @param orderId
	 * @param tableId
	 */
	public void checkout(Long orderId, Long tableId) {
		ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
		Connection conn = null;
		try {
			conn = JdbcUtils.getDataSource().getConnection();		
			tl.set(conn); 
			conn.setAutoCommit(false);
			this.orderDao.updateOrderStatus(orderId, OrderStatus.PAYED);
//			this.dinnerTableService.quitTable(tableId);
			conn.commit();
		} catch (SQLException ex) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ex.printStackTrace();
		} finally {
			if(conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}			
		}
	}
}
